package cn.cai.controller;


import cn.cai.commonUtils.Result;
import cn.cai.pojo.EduChapter;
import cn.cai.pojo.vo.ChapterVo;
import cn.cai.service.IEduChapterService;
import cn.cai.service.IEduCourseService;
import cn.cai.service.IEduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author caishenao
 * @since 2021-08-17
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Api(value = "EduChapter的Controller层实现",tags = "课程操作的信息")
public class EduChapterController {

    @Autowired
    private IEduChapterService chapterService;

    //课程大纲列表，根据课程id进行查询
    @ApiOperation(value ="根据课程id查询课程全部信息")
    @GetMapping("/getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable("courseId") String courseId){
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return Result.success().data("list",list);
    }

    //添加章节
    @ApiOperation(value = "添加章节")
    @PostMapping("/addChapter")
    public Result addChapter(@RequestBody @ApiParam(name = "eduChapter" ,value = "上传的章节信息") EduChapter eduChapter){
        chapterService.save(eduChapter);
        return Result.success().data("msg","添加成功");
    }


    //根据章节id查询
    @ApiOperation(value = "根据章节id查询章节信息")
    @GetMapping("/queryChapter/{chapterId}")
    public Result queryChapter(@PathVariable("chapterId")
                                           String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return Result.success().data("chapter",chapter);
    }

    //修改章节信息
    @ApiOperation(value = "修改章节信息")
    @PutMapping("/update")
    public Result update(@RequestBody
                             @ApiParam(name = "eduChapter",value = "修改的章节信息")
                                     EduChapter eduChapter){
        boolean b = chapterService.updateById(eduChapter);
        if (b){
            return Result.success().data("msg","修改成功");
        }else {
            return Result.error();
        }
    }

    //删除章节信息  没有删除章节下的视频
    @ApiOperation(value = "删除章节信息，没有删除章节下的视频")
    @DeleteMapping("/delete/{chapterId}")
    public Result delete(@PathVariable("chapterId")
                             @ApiParam(name = "chapterId",value = "要删除章节id")
                                     String chapterId){
        boolean b = chapterService.removeById(chapterId);
        //三元运算符
        return b?Result.success().data("msg","删除成功"):Result.error();
    }

    //删除章节信息，将章节下对应的视频（小节）全部删除
    @ApiOperation("删除章节信息，将章节下对应的视频（小节）全部删除")
    @DeleteMapping("/deleteAll/{chapterId}")
    public Result deleteAll(@PathVariable("chapterId")
                                @ApiParam(name = "chapterId",value = "删除的章节id")
                                        String chapterId){
        boolean b = chapterService.deleteAndVideo(chapterId);
        return b?Result.success().data("msg","删除成功"):Result.error();
    }

    //删除课程信息，如果课程章节下有小节，返回错误信息
    @ApiOperation("删除章节信息，章节下不能含有小节")
    @DeleteMapping("/deleteOnlyChapter/{chapterId}")
    public Result deleteOnlyChapter(@PathVariable("chapterId")
                                        @ApiParam(name = "chapterId",value = "删除的章节id")
                                                String chapterId){
        chapterService.deleteOnlyChapter(chapterId);
        return Result.success().data("msg","章节删除成功");
    }


}
